﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OA.Model;
using OA.Manager;
using System.Globalization;
using OA.Utility;

namespace OA.Bll
{
    public class DictionaryTypePresenter
    {
        public string Add(JsonReq req)
        {
            DictionaryTypeInfo info = new DictionaryTypeInfo { TypeName = req.Options["TypeName"], TypeText = req.Options["TypeValue"] };
            DictionaryTypeManager dtm = new DictionaryTypeManager();
            bool bl = dtm.InsertDictionaryTypeInfo(info);
            string strmsg = "添加类型成功!";
            return "{ \"flag\":\"" + bl.ToString(CultureInfo.InvariantCulture) + "\",\"msg\":\"" + strmsg + "\"}";
        }
        public string Paged(JsonReq req)
        {
            int pageIndex = Convert.ToInt32(req["page"]);
            int pageSize = Convert.ToInt32(req["rows"]);
            int total;
            DictionaryTypeManager manager = new DictionaryTypeManager();
            List<DictionaryTypeInfo> list = manager.GetDictionaryTypeInfoAll(pageIndex, pageSize, out total);
            //  int total = manager.GetDictionaryTypeTotal();
            string result = JsonHelper.ListToGridJson<DictionaryTypeInfo>(list, total);
            return result;
        }
        public string GetDictionaryTypeInfoAll()
        {
            List<DictionaryTypeInfo> list = new DictionaryTypeManager().GetDictionaryTypeInfoAll();
            StringBuilder sb = new StringBuilder();
            sb.Append("[");
            foreach (DictionaryTypeInfo dictionaryInfo in list)
            {
                sb.Append("{");
                if (dictionaryInfo != null)
                    sb.Append(string.Format("\"id\": \"{0}\", \"text\": \"{1}\"",
                                            dictionaryInfo.Id.ToString(CultureInfo.InvariantCulture),
                                            dictionaryInfo.TypeName));
                sb.Append("},");
            }
            sb.Remove(sb.ToString().Length - 1, 1);
            sb.Append("]");
            return sb.ToString();
        }
    }
}
